package com.wlyuan.open.domain.api.service;

import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.mybatis.query.PageUtils;
import com.wlyuan.open.domain.api.OpenApi;
import com.wlyuan.open.domain.api.OpenApiInfo;
import com.wlyuan.open.domain.api.respository.OpenApiInfoRepository;
import com.wlyuan.open.domain.api.respository.OpenApiRepository;
import com.wlyuan.open.domain.api.valueobj.OpenApiId;
import com.wlyuan.open.domain.client.OpenClientId;
import com.wlyuan.open.dto.api.OpenApiDocFilterDTO;
import com.wlyuan.open.dto.api.OpenApiFilterDTO;
import com.wlyuan.utils.Page;
import com.wlyuan.utils.Sort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lijing
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OpenApiService {
    private final OpenApiRepository openApiRepository;
    private final OpenApiInfoRepository openApiInfoRepository;

    /**
     * 根据appId获取api信息
     *
     * @param openClientId
     * @return
     */
    public List<OpenApi> getOpenApiByClientId(OpenClientId openClientId) {
        //获取客户端所有接口
        var apiList = openApiInfoRepository.getByClientId(openClientId);
        //获取客户端所申请的api接口信息
        var apiIdList = apiList.stream().map(OpenApiInfo::getApiId).map(apiId -> new OpenApiId(apiId)).collect(Collectors.toList());
        var list = openApiRepository.list(apiIdList);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }

    /**
     * 获取所有有效的api
     *
     * @return
     */
    public List<OpenApi> getApiDocList(OpenApiDocFilterDTO filter) {
        OpenApiFilterDTO filterDTO = resolverFilter(filter);
        var page = openApiRepository.search(filterDTO);
        if (page == null || CollectionUtils.isEmpty(page.getRecords())) {
            return Collections.emptyList();
        }
        return openApiRepository.list(page.getRecords());
    }

    private OpenApiFilterDTO resolverFilter(OpenApiDocFilterDTO filter) {
        //获取所有启用的api
        OpenApiFilterDTO filterDTO = new OpenApiFilterDTO();
        //有效的
        filterDTO.setStatus(NamedStatus.ENABLED.getValue());
        filterDTO.setName(filter.getApiName());
        filterDTO.setPage(1L);
        filterDTO.setSize(1000L);
        //权重排序规则
        List<Sort> sortList = new ArrayList<>();
        Sort sort = new Sort();
        sort.setName("sort");
        sort.setAsc(false);
        sortList.add(sort);
        filterDTO.setSort(sortList);
        return filterDTO;
    }

    /**
     * 获取所有有效api列表，分页
     *
     * @param filterDTO
     * @return
     */
    public Page<OpenApi> getAllInvalidPageApi(OpenApiFilterDTO filterDTO) {
        //有效的
        filterDTO.setStatus(NamedStatus.ENABLED.getValue());
        var result = openApiRepository.search(filterDTO);
        return PageUtils.as(result, openApiRepository.list(result.getRecords()));
    }


    public void remove(OpenClientId openClientId) {
        openApiInfoRepository.removeByClient(openClientId);
    }
}
